﻿/// <reference path="jquery.min.js" />
/// <reference path="layer/layer.js" />

$(function () {
    //启动提示
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: ['450px', '250px'],
        skin: 'yourclass',
        content: $(".dtalert")
    });
   
    //postifmt[0] = "1,2,3,4,5,,";
    //if (test[0].split(",")[6] == "") {
    //    alert("ok");
    //}
    //for (var i = 0; i < 7; i++) {
    //    alert(test[0].split(",")[i]);
    //}
    function testMode(){
        listSize = 50;
        postifmt = new Array(listSize);
        ii = (listSize - 1) * 200;
        nowlist = listSize - ((listSize * 200 - ii) / 200);
        arrphoto = new Array(listSize);
        arrxuehao = new Array(listSize);
        stuifmt = new Array(listSize);
        tArray = new Array();  //先声明一维
        i = listSize, p = 5;
        for (var k = 0; k < i; k++) {    //一维长度为i,i为变量，可以根据实际情况改变

            tArray[k] = new Array();  //声明二维，每一个一维数组里面的一个元素都是一个数组；

            for (var j = 0; j < p; j++) {   //一维数组里面每个元素数组可以包含的数量p，p也是一个变量；

                tArray[k][j] = "";    //这里将变量初始化，我这边统一初始化为空，后面在用所需的值覆盖里面的值
            }
        }
        $(".photo ul").css({ top: "-" + (listSize - 1) * 200 + "px" });
    }

    var listSize = 20;
    var postifmt = new Array(listSize);
    var ii = (listSize-1)*200;
    var nowlist = listSize - ((listSize*200 - ii) / 200);
    var rotate = 0;
    var arrphoto = new Array(listSize);
    var arrxuehao = new Array(listSize);
    var stuifmt = new Array(listSize);
    var tArray = new Array();  //先声明一维
    var i = listSize, p = 5;
    for (var k = 0; k < i; k++) {    //一维长度为i,i为变量，可以根据实际情况改变

        tArray[k] = new Array();  //声明二维，每一个一维数组里面的一个元素都是一个数组；

        for (var j = 0; j < p; j++) {   //一维数组里面每个元素数组可以包含的数量p，p也是一个变量；

            tArray[k][j] = "";    //这里将变量初始化，我这边统一初始化为空，后面在用所需的值覆盖里面的值
        }
    }
    //tArray[0][0] = "123,456,789";
    //tArray[0][1] = "234,456,789";
    //alert(tArray[0][0] + "|" + tArray[0][1]);
    $(".photo ul").css({top:"-"+(listSize-1)*200+"px"});

    // 定义存放生成随机数的数组 
    var array = new Array();
    //生成随机数
    function randsum() {
        array.length = 0;
        // 循环N次生成随机数 
        for (var i = 0 ; ; i++) {
            // 只生成5个随机数 
            if (array.length < 5) {
                generateRandom(5);
            } else {
                break;
            }
        }
        // 生成随机数的方法 
        function generateRandom(count) {
            var rand = parseInt(Math.random() * count);
            for (var i = 0 ; i < array.length; i++) {
                if (array[i] == rand) {
                    return false;
                }
            }
            array.push(rand);
        }
    }
    //$.ajax({
    //    url: "request.ashx?flag"+Math.random(),
    //    type: "post",
    //    data: { cmd: "photo" },
    //    success: function (data) {
    //        var json = eval(data);
    //        alert(json.length);
    //        //for(var j =0)
    //        }

    //});
    //$.post("request.ashx?flag=" + Math.random(), { cmd: "photo" }, function (img) {
    //    var json = eval(img);
    //    for (var i = 0; i < json.length; i++) {
    //        $(".photo ul").prepend('<li class="img"><div class="zxz">←</div><div class="fd">+</div><img src="' + json[i]["photo"] + '" style="display:none;" /><div class="yxz">→</div></li>');
    //    }
    //});

    //打开登录窗口
    $("#login a").click(function () {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            shadeClose: true,
            skin: 'yourclass',
            area: '520',
            content: $("#loginbox")
        });
    });
    var fid = 0;
    //登录提交
    $(".lgsub").click(function () {
        var uid = $(".lguser input").val();
        var pwd = $(".lgpass input").val();
        $.ajax({
            url: "admin.ashx?rand=" + Math.random(),
            type: "post",
            data: {cmd:"login", uid: uid, pwd: pwd },
            success: function (d) {
                if (d != "no") {
                    var json = eval(d);
                    layer.closeAll();
                    layer.msg("欢迎您登录，" + json[0]["name"] + "老师!");
                    fid = json[0]["fid"];
                    $("#login a").text(json[0]["name"]).unbind();
                    $("#gongyulou").find("option:selected").text(json[0]["gongyu"]);
                    layer.confirm('请选择模式', {
                        btn: ['正式考核', '题目练习'] //按钮
                    }, function () {
                        start(json[0]["gongyu"], 0);
                        layer.alert("本次考核共二十个同学，全部答题完毕后请点击提交按钮。");
                    }, function () {
                        testMode();
                        start(json[0]["gongyu"], 1);
                        layer.alert("本次考核共二十个同学，全部答题完毕后请点击提交按钮，练习模式不可提交");
                    });
                    
                }
                else {
                    layer.alert("帐号或密码错误");
                }
            }
        });
    });
    //确定点击
    //$("#queding").click(function () {
    //    alert();
    //});
    //var countifmt = new array();

    function start(gongyul,mode) {
        //获取照片
        $.ajax({
            url: "request.ashx?flag=" + Math.random(),
            type: "post",
            data: { cmd: "photo", gongyu: gongyul,pagesize:listSize },
            async: false,
            success: function (img) {
                var json = eval(img);
                for (var i = 0; i < json.length; i++) {
                    arrphoto[i] = json[i]["photo"];
                    arrxuehao[i] = '2017_img/1516/' + json[i]["xuehao"] + '.jpg';
                    stuifmt[i] = json[i]["name"] + "," + json[i]["banji"] + "," + json[i]["zhuanye"] + "," + json[i]["qinshihao"];
                    $(".photo ul").prepend('<li class="img"><div class="zxz">←</div><div class="fd">+</div><img src="" style="display:none;" alt="资助与奖励：' + json[i]["msg1"] + "<br>获奖情况：" + json[i]["msg2"] + '" /><div class="yxz">→</div></li>');
                }
                $(".photo .img img").eq(listSize-1).attr("src", arrxuehao[0]).
                attr("index", 0).
                attr("onerror", "javascript:this.src='" + arrphoto[listSize-1 - (nowlist)].replace('\\', '\\\\') + "'");
                var description = $(".photo .img img").eq(listSize-1).attr("alt").split('<br>');
                $(".testbox .ppmsg").text("").text(description[0]).append('<br />' + '<p>' + description[1] + '</p>');
                randstu();
                //alert(stuifmt[0]);
            }
        });
        
        //随机获取20个学生4条虚拟数据
        function randstu() {
            $.ajax({
                url: "request.ashx?flag=" + Math.random(),
                type: "post",
                data: { cmd: "randsum", pagesize: listSize },
                async: false,
                success: function (d) {
                    var json = eval(d);
                    for (var i = 1; i <= listSize*4; i++) {
                        if (i % 4 == 0) {
                            for (var j = i; j >= i - 3; --j) {
                                tArray[i / 4 - 1][j % 4] = json[j - 1]["name"] + "," + json[j - 1]["banji"] + "," + json[j - 1]["zhuanye"] + "," + json[j - 1]["qinshihao"];
                            }
                            tArray[i / 4 - 1][4] = stuifmt[i / 4 - 1];
                        }
                    }
                }
            });
        }
        apprandstu(0,mode);
        //for (var i = 0; i < 19; i++) {
        //    alert(tArray[i][0]);
        //}
        $(".photo .img").eq(nowlist).find("img").removeAttr("style");
        //图片旋转
        $(".photo .img .zxz").click(function () {
            rotate -= 90;
            $(this).parent().find("img").css({ "transform": "rotate(" + rotate + "deg)" });
        });
        $(".photo .img .yxz").click(function () {
            rotate += 90;
            $(this).parent().find("img").css({ "transform": "rotate(" + rotate + "deg)" });
        });
        //图片放大
        $(".photo .img .fd").click(function () {
            $("#imgfd img").attr({ "src": $(this).siblings("img").attr("src"), "style": $(this).siblings("img").attr("style") });
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                shadeClose: true,
                skin: 'yourclass',
                area: ['520px', '95%'],
                content: $("#imgfd")
            });
        });
        //下一张图片
        $("#down").click(function () {
            savaifmt(nowlist);
            //alert(postifmt[nowlist]);

            if (ii == 0) {
                ii = (listSize-1)*200;
            }
            else {
                ii = ii - 200;
            }
            nowlist = listSize - ((listSize * 200 - ii) / 200);
            var obj = $(".photo .img").eq(nowlist).find("img");
            obj.removeAttr("style");
            obj.attr("src", arrxuehao[listSize-1 - (nowlist)]).
            attr("index", listSize-1 - (nowlist)).
            attr("onerror", "javascript:this.src='" + arrphoto[listSize-1 - (nowlist)].replace('\\','\\\\') + "'");
            //alert(stuifmt[19 - (nowlist)] + "|" + tArray[19 - (nowlist)][4]);
            //if (postifmt[savaifmt(nowlist)])
            //如有填写读取下一个学生数据
            if (postifmt[nowlist] != undefined) {
                //.find("option[text='pxx']").attr("selected",true);
                //$("#list-name").find("option").eq(0).text(postifmt[nowlist].split("-")[1]);
                apprandstu(listSize-1 - (nowlist), mode);
                $("#list-name").val(postifmt[nowlist].split("-")[1]);//姓名
                $("#list-banji").val(postifmt[nowlist].split("-")[2]);//班级
                $("#list-zhuanye").val(postifmt[nowlist].split("-")[3]);//专业
                $("#list-qinshihao").val(postifmt[nowlist].split("-")[4]);//寝室号
                for (var i = 0; i < postifmt[nowlist].split("-")[5].split("|").length; i++) {
                    if (postifmt[nowlist].split("-")[5].split("|")[i] != "") {
                        $("input[value='" + postifmt[nowlist].split("-")[5].split("|")[i] + "']").attr("checked", "checked");
                    }
                }
                for (var i = 0; i < postifmt[nowlist].split("-")[6].split("|").length; i++) {
                    if (postifmt[nowlist].split("-")[6].split("|")[i] != "") {
                        $("input[value='" + postifmt[nowlist].split("-")[6].split("|")[i] + "']").attr("checked", "checked");
                    }
                }
            }
            else
            {
                //插入随机数据
                apprandstu(listSize-1 - (nowlist),mode);
            }
            var description = obj.attr("alt").split('<br>');
            $(".testbox .ppmsg").text("").text(description[0]).append('<br />' + '<p>' + description[1] + '</p>');
            $(".photo ul").animate({ "top": "-" + ii + "px" }, 200);
            $(".stusum").text(listSize - (ii / 200));
            rotate = 0;
        });
        //上一张图片
        $("#up").click(function () {
            savaifmt(nowlist);
            //alert(postifmt[nowlist]);

            if (ii == (listSize - 1) * 200) {
                ii = 0;
            }
            else {
                ii = ii + 200;
            }
            nowlist = listSize - ((listSize * 200 - ii) / 200);
            var obj = $(".photo .img").eq(nowlist).find("img");
            obj.removeAttr("style");
            obj.attr("src", arrxuehao[listSize-1 - (nowlist)]).
            attr("index", listSize-1 - (nowlist)).
            attr("onerror", "javascript:this.src='" + arrphoto[listSize-1 - (nowlist)].replace('\\', '\\\\') + "'");
            //alert(stuifmt[19 - (nowlist)] + "|" + tArray[19 - (nowlist)]);
            //如有填写读取下一个学生数据
            if (postifmt[nowlist] != undefined) {
                //$("#list-name").find("option").eq(0).text(postifmt[nowlist].split("-")[1]);
                apprandstu(listSize-1 - (nowlist), mode);
                $("#list-name").val(postifmt[nowlist].split("-")[1]);//姓名
                $("#list-banji").val(postifmt[nowlist].split("-")[2]);//班级
                $("#list-zhuanye").val(postifmt[nowlist].split("-")[3]);//专业
                $("#list-qinshihao").val(postifmt[nowlist].split("-")[4]);//寝室号
                for (var i = 0; i < postifmt[nowlist].split("-")[5].split("|").length; i++) {
                    if (postifmt[nowlist].split("-")[5].split("|")[i] != "") {
                        $("input[value='" + postifmt[nowlist].split("-")[5].split("|")[i] + "']").attr("checked", "checked");
                    }
                }
                for (var i = 0; i < postifmt[nowlist].split("-")[6].split("|").length; i++) {
                    if (postifmt[nowlist].split("-")[6].split("|")[i] != "") {
                        $("input[value='" + postifmt[nowlist].split("-")[6].split("|")[i] + "']").attr("checked", "checked");
                    }
                }
            }
            else {
                //插入随机数据
                apprandstu(listSize-1 - (nowlist), mode);
            }
            var description = obj.attr("alt").split('<br>');
            $(".testbox .ppmsg").text("").text(description[0]).append('<br />' + '<p>' + description[1] + '</p>');
            $(".photo ul").animate({ "top": "-" + ii + "px" }, 200);
            $(".stusum").text(listSize-1 - (ii / 200));
            rotate = 0;
        });
        //点击提交
        $("#tijiao").click(function () {
            savaifmt(nowlist);
            var j = 0;
            for (var i = 0; i < listSize; i++) {
                if (postifmt[i] == undefined){
                    j++;
                }
            }
            if (j != 0) {
                layer.alert("您还有"+j+"学生未答题,请点击下一个按钮进行查看！");
            }else {
                $.post("request.ashx", { cmd: "postifmt",fid:fid, data: JSON.stringify(postifmt) }, function (a) {
                    layer.alert(a);
                });
            }
        });
        function savaifmt(ii){
            //提交数据
            //var imgsrc = $(".photo .img img").eq(ii).attr("src");
            var imgsrc = arrphoto[$(".photo .img img").eq(ii).attr("index")];
            var xm = $("#list-name").find(":checked").text();//姓名
            var bj = $("#list-banji").find(":checked").text();//班级
            var zy = $("#list-zhuanye").find(":checked").text();//专业
            var qs = $("#list-qinshihao").find(":checked").text();//寝室号
            var zx = "";//在校担任
            $("input[name='ganbu']").each(function () {
                if ($(this).attr("checked") == "checked") {
                    zx = zx + "|" + $(this).val();
                }
                else {
                    zx = zx + "|" + "";
                }
            });
            var gz = "";//重点关注
            $("input[name='guanzhu']").each(function () {
                if ($(this).attr("checked") == "checked") {
                    gz = gz + "|" + $(this).val();
                }
                else {
                    gz = gz + "|" + "";
                }
            });
            var pp = $("[name='ppdc']:checked").val();
            $("[name='ganbu']:checked").removeAttr("checked");
            $("[name='guanzhu']:checked").removeAttr("checked");
            $("[name='ppdc']:checked").removeAttr("checked");
            $("[name='ppdc']").eq(2).attr("checked", "checked");
            postifmt[ii] = imgsrc + "-" + xm + "-" + bj + "-" + zy + "-" + qs + "-" + zx + "-" + gz + "-" + pp;
        }
        //插入随机数据
        //插入随机数据
        function apprandstu(e, flag) {
            if (!flag) {

                //取随机姓名
                randsum();
                $("#list-name").html('<option value ="" >请选择</option>');
                for (var z = 0; z < 5; z++) {
                    $("#list-name").append('<option value ="' + tArray[e][array[z]].split(",")[0] + '">' + tArray[e][array[z]].split(",")[0] + '</option>');
                }
                //取随机班级
                $("#list-banji").html('<option value ="" >请选择</option>');
                for (var z = 0; z < 5; z++) {
                    $("#list-banji").append('<option value ="' + tArray[e][array[z]].split(",")[1] + '">' + tArray[e][array[z]].split(",")[1] + '</option>');
                }
                //取随机专业
                $("#list-zhuanye").html('<option value ="" >请选择</option>');
                for (var z = 0; z < 5; z++) {
                    $("#list-zhuanye").append('<option value ="' + tArray[e][array[z]].split(",")[2] + '">' + tArray[e][array[z]].split(",")[2] + '</option>');
                }
                //取随机寝室号
                $("#list-qinshihao").html('<option value ="" >请选择</option>');
                for (var z = 0; z < 5; z++) {
                    $("#list-qinshihao").append('<option value ="' + tArray[e][array[z]].split(",")[3] + '">' + tArray[e][array[z]].split(",")[3] + '</option>');
                }
            } else {
                //取随真实数据
                randsum();
                $("#list-name").html('<option value ="' + tArray[e][4].split(",")[0] + '">' + tArray[e][4].split(",")[0] + '</option>');

                $("#list-banji").html('<option value ="' + tArray[e][4].split(",")[1] + '">' + tArray[e][4].split(",")[1] + '</option>');

                $("#list-zhuanye").html('<option value ="' + tArray[e][4].split(",")[2] + '">' + tArray[e][4].split(",")[2] + '</option>');

                $("#list-qinshihao").html('<option value ="' + tArray[e][4].split(",")[3] + '">' + tArray[e][4].split(",")[3] + '</option>');

            }
        }
    }
});